package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Course;
import com.qk.enums.ParamEnum;
import com.qk.exception.CommonException;
import com.qk.management.mapper.CourseMapper;
import com.qk.management.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 *
 * @author lyl
 * @created 2025/10/9 14:34
 * @description
 *
 */
@Service
@SuppressWarnings("all")
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;
    @Override
    public PageResult<Course> getListByPage(String name, Integer subject, Integer target, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Course> list = courseMapper.getListByPage(name, subject, target);
        Page<Course> pageResult = (Page<Course>)  list;
        return PageResult.<Course>builder()
                .total(Long.valueOf(pageResult.getTotal()).intValue())
                .rows(pageResult.getResult())
                .build();
    }

    @Override
    public void add(Course course) {
        boolean hasNull = BeanUtil.hasNullField(course, "id", "description", "createTime", "updateTime");
        if (hasNull) {
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);

        }
        course.setUpdateTime(LocalDateTime.now());
        course.setCreateTime(LocalDateTime.now());
        courseMapper.add(course);
    }

    @Override
    public void delete(Integer id) {
        courseMapper.delete(id);
    }

    @Override
    public Course getCourseById(Integer id) {
        return courseMapper.getCourseById(id);
    }

    @Override
    public void update(Course course) {
        boolean hasNull = BeanUtil.hasNullField(course, "description", "updateTime");
        if (hasNull) {
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.update(course);

    }

    @Override
    public List<Course> getAllList() {
        return courseMapper.getAllList();
    }

    @Override
    public List<Course> getListBySubject(Integer subject) {
        return courseMapper.getListBySubject(subject);
    }
}
